﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.IO;
namespace TinyCinema.Data
{
    public class Customer : Execution
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string Address { get; set; }
        public long Phone { get; set; }
        public string Email { get; set; }
        public long CardNo { get; set; }
        public int Point { get; set; }
        public DateTime RegDate { get; set; }
        public CustomerType Type { get; set; }
        bool IsIniting = true;
        public string _Password;
        public string Password
        {
            get { return _Password; }
            set
            {
                if (!IsIniting)
                {
                    _Password = Encryption.CreateMD5Hash(value);
                    Save();
                }
                else _Password = value;
            }
        }
        public ObservableCollection<BookedTicket> BookedTickets { get; set; }
        public ObservableCollection<TicketOrder> TicketOrders { get; set; }
        public ObservableCollection<MenuOrder> MenuOrders { get; set; }
        public Customer() {
            Type = new CustomerType();
            ID = IDGenerate(Tables.Customer);
            IsIniting = false;
            RegDate = DateTime.Now;
            Point = 0;
        }
        public Customer(string _ID)
        {
            ID = _ID;
            PutTableIntoDataSet(Tables.Customer);
            var tbl = ds.Tables[Tables.Customer];
            var Row = tbl.Select(CustomerTable.ID + "='" + ID + "'")[0];
            Name = Row[CustomerTable.Name] as String;
            Gender = Row[CustomerTable.Gender] as String;
            DateOfBirth = (DateTime)Row[CustomerTable.DateOfBirth];
            Address = Row[CustomerTable.Address] as String;
            Phone = long.Parse(Row[CustomerTable.Phone] as String);
            Email = Row[CustomerTable.Email] as String;
            CardNo =long.Parse(Row[CustomerTable.CardNo] as String);
            RegDate = (DateTime)Row[CustomerTable.RegDate];
            Point = (int)Row[CustomerTable.Point];
            Type = new CustomerType(Row[CustomerTypeTable.ID] as String);
            Password = Row[CustomerTable.Password] as String;
            IsIniting = false;
        }
        public static ObservableCollection<Customer> GetAllCustomer()
        {
            PutTableIntoDataSet(Tables.Customer);
            var tbl = ds.Tables[Tables.Customer];
            var Rows = tbl.Select(CustomerTable.Point + " > = " + "0");
            var Result = new ObservableCollection<Customer>();
            foreach (var Row in Rows)
            {
                
                var customer = new Customer(Row[CustomerTable.ID] as string);
                Result.Add(customer);
            }
            return Result;
        }
        public void Save()
        {
            using (var tbl = ds.Tables[Tables.Customer])
            {
                var Rows = ds.Tables[Tables.Customer].Select(CustomerTable.ID + "='" + ID + "'");
                if (Rows.Length == 0)
                {
                    var Row = tbl.NewRow();
                    Row[CustomerTable.ID] = ID;
                    Row[CustomerTable.Name] = Name;
                    Row[CustomerTable.Gender] = Gender;
                    Row[CustomerTable.DateOfBirth] = DateOfBirth;
                    Row[CustomerTable.Address] = Address;
                    Row[CustomerTable.Phone] = Phone;
                    Row[CustomerTable.Email] = Email;
                    Row[CustomerTable.CardNo] = CardNo;
                    Row[CustomerTable.RegDate] = RegDate;
                    Row[CustomerTable.CustomerTypeID] = Type.ID;
                    Row[CustomerTable.Password] = Password;
                    Row[CustomerTable.Point] = Point;
                    tbl.Rows.Add(Row);
                    SaveCommand(Tables.Customer);
                    
                }
                else
                {
                    foreach (System.Data.DataRow Row in tbl.Rows)
                        if ((Row[CustomerTable.ID] as String) == ID)
                        {
                            Row[CustomerTable.Name] = Name;
                            Row[CustomerTable.Gender] = Gender;
                            Row[CustomerTable.DateOfBirth] = DateOfBirth;
                            Row[CustomerTable.Address] = Address;
                            Row[CustomerTable.Phone] = Phone;
                            Row[CustomerTable.Email] = Email;
                            Row[CustomerTable.CardNo] = CardNo;
                            Row[CustomerTable.RegDate] = RegDate;
                            Row[CustomerTable.CustomerTypeID] = Type.ID;
                            Row[CustomerTable.Password] = Password;
                            Row[CustomerTable.Point] = Point;
                            SaveCommand(Tables.Customer);
                        }
                }
            }
        }
        public void Delete()
        {
            using (var tbl= ds.Tables[Tables.Customer])
            {
                string Condition = CustomerTable.ID + "='" + ID + "'";
                tbl.Select(Condition)[0].Delete();
                SaveCommand(Tables.Customer);
            }
        }
        public static ObservableCollection<Customer> SearchByName(string str)
        {
            PutTableIntoDataSet(Tables.Customer);
            var tbl = ds.Tables[Tables.Customer];
            var Rows = tbl.Select(CustomerTable.Name + " Like '%" + str + "%'");
            var Result = new ObservableCollection<Customer>();
            foreach (var Row in Rows)
            {
                Result.Add(new Customer(Row[CustomerTable.ID] as String));
            }
            return Result;
        }

        public static ObservableCollection<Customer> SearchByNo(string str)
        {
            PutTableIntoDataSet(Tables.Customer);
            var tbl = ds.Tables[Tables.Customer];
            var Rows = tbl.Select(CustomerTable.ID + " Like '%" + str + "%'");
            var Result = new ObservableCollection<Customer>();
            foreach (var Row in Rows)
            {
                Result.Add(new Customer(Row[CustomerTable.ID] as String));
            }
            return Result;
        }
        public ObservableCollection<TicketOrder> GetAllTicketOrder()
        {
            PutTableIntoDataSet(Tables.TicketOrder);
            var tbl = ds.Tables[Tables.TicketOrder];
            try
            {
                var Rows = tbl.Select(TicketOrderTable.CustomerID + "= '" + ID + "'");
            
                var Result = new ObservableCollection<TicketOrder>();
                foreach (var Row in Rows)
                {
                    Result.Add(new TicketOrder(Row[TicketOrderTable.ID] as string));
                }
                return Result;
            }
            catch { }
            return null;
        }
        public ObservableCollection<BookedTicket> GetAllBookedTicket()
        {
            PutTableIntoDataSet(Tables.BookedTicket);
            var tbl = ds.Tables[Tables.BookedTicket];
            try
            {
                var Rows = tbl.Select(BookedTicketTable.CustomerID + "= '" + ID + "'");

                var Result = new ObservableCollection<BookedTicket>();
                foreach (var Row in Rows)
                {
                    Result.Add(new BookedTicket(Row[BookedTicketTable.ID] as string));
                }
                return Result;
            }
            catch { }
            return null;
        }
        public ObservableCollection<MenuOrder> GetAllMenuOrder()
        {
            PutTableIntoDataSet(Tables.MenuOrder);
            var tbl = ds.Tables[Tables.MenuOrder];
            try
            {
                var Rows = tbl.Select(MenuOrderTable.CustomerID + "= '" + ID + "'");

                var Result = new ObservableCollection<MenuOrder>();
                foreach (var Row in Rows)
                {
                    Result.Add(new MenuOrder(Row[MenuOrderTable.ID] as string));
                }
                return Result;
            }
            catch { }
            return null;
        }
    }
}
